package com.orhon.smartcampus.modules.systemctl.fetcher;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDutiesDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartment;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDuty;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DutiesMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class DutiesFetcher implements DataFetcher {


    @Autowired
    OrgDutiesDao orgDutiesDao;

    /**
     * 获取部门列表
     *
     * @param environment
     * @return
     * @throws Exception editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        List<GQLDuty> duties = new ArrayList<>();

        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(environment);
        String hql = "from OrgDuties where deleted_at = null ";

        if (environment.containsArgument("condition")) {
            HashMap<String, Object> map = ObjectToMap.to(environment.getArgument("condition"));
            if (map.get("id") != null) {
                hql += "and id = " + map.get("id");
            }
            if (map.get("parent_id") != null) {
                hql += "and parent_id " + Util.decide(map.get("parent_id"));
            }

            if (map.get("duties_name") != null) {
                hql += "and duties_name " + Util.decide(map.get("duties_name"));
            }

        }
        List<OrgDuties> list = orgDutiesDao.find(hql, null, page.getPage(), page.getLimit());


        for (OrgDuties item : list) {
//            GQLDepartment gqlDepartment = null;
//            if (environment.getSelectionSet().contains("data/parent")) {
//                OrgDepartments parent = item.getSystemctlOrgDepartmentsByParentId();
//                gqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(parent);
//            }
            GQLDuty duty = DutiesMapper.MAPPER.toOrgDuties(item);
//            department.setSchool(gqlSchool);
            duties.add(duty);
        }

        Long total = 0L;
        if (environment.getSelectionSet().contains("total")) {
            total = orgDutiesDao.getTotalCount(hql);
        }
        return Result.ok().put("data", duties).put("total", total);
    }
}
